﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLogicLayer
{
    public class AdjustmentVoucherFacade
    {
        
        InventorySoreDataContext context;
        AdjustmentVoucher voucher;
        public AdjustmentVoucherFacade()
        {
            context = new InventorySoreDataContext();
        }
        public IQueryable FindAllPendingAdjustmentVoucher()
        {
            var vouchers = (from adjVoucher in context.AdjustmentVouchers where adjVoucher.Status=="Pending" select new { 
                                VoucherID=adjVoucher.VoucherID,
                                DateIssue=adjVoucher.DateIssue,
                                ClerkName=adjVoucher.Employee.EmployeeName,
                                Price=adjVoucher.TotalPrice
                               
                            });
            return vouchers;
        }
        public AdjustmentVoucher FindAdjustmentVoucherByVoucherID(string voucherID)
        {
            var vouchers = (from adjVoucher in context.AdjustmentVouchers where adjVoucher.VoucherID == voucherID select adjVoucher).Single();
            voucher = vouchers;
            return voucher;
        }

        public IQueryable FindAdjustmentVoucherDetailsByVoucherID(string voucherID)
        {
            var voucherdetails = (from adjVoucherDetails in context.AdjustmentVoucherDetails
                                  where adjVoucherDetails.VoucherID == voucherID
                                  select new { 
                                      ItemCode=adjVoucherDetails.ItemCode,
                                      AdjustmentQty=adjVoucherDetails.Operation+adjVoucherDetails.AdjustedQty.ToString(),
                                      Reason=adjVoucherDetails.Reason,
                                      Price=adjVoucherDetails.SuggestedPrice,
                                  });
            return voucherdetails;
        }

        public void UpdateAdjustmentVoucher(string voucherID, string status,string approvedBy)
        {
            voucher = FindAdjustmentVoucherByVoucherID(voucherID);
            voucher.VoucherID = voucherID;
            voucher.Status = status;
            voucher.ApproveBy = approvedBy;
            context.SubmitChanges();
        }

        public float CalculateTotalPrice(List<AdjustmentVoucherDetail> adjlist)
        {
            float total = 0.0F;

            foreach (AdjustmentVoucherDetail ad in adjlist)
            {
                total +=(float) Convert.ToDouble(ad.SuggestedPrice);
            }

            return total;

        }

        public string GetAdjustmentID(string clerkId)
        {
            try
            {
                string newFormID;
                string pre = "AdjV";
                var query = (from adjust in context.AdjustmentVouchers
                            
                             select adjust);
                if (query.Count() != 0)
                {
                    int code = query.Count();
                    code++;
                    code = code + 100;

                    newFormID = pre + "/" + code + "/" + Convert.ToString(DateTime.Now.Year);
                }
                else
                {
                    newFormID = pre + "/100/" + Convert.ToString(DateTime.Now.Year);
                }
                return newFormID;
            }
            catch (Exception ee)
            {
                return null;
            }
        }


        public void insertAdjustment(AdjustmentVoucher adjustmentVoucher, List<AdjustmentVoucherDetail> adju)
        {
            
                context.AdjustmentVouchers.InsertOnSubmit(adjustmentVoucher);
                context.AdjustmentVoucherDetails.InsertAllOnSubmit(adju);
                context.SubmitChanges();
            

        }

        public void SendMail(string empID,float total)
        {


            string from = new EmployeeFacade().FindEmployeeByID(empID).Email;
            string to = null;
            string body = "One Adjustment Voucher is Waiting for your approval.";
            string subject = "Status of Adjustment Voucher";
            
            if (total <= 250)
            {
                to = new EmployeeFacade().FindEmail("STNT", "Store Manager");
               
            }
            else
            {
                to = new EmployeeFacade().FindEmail("STNT", "Store Supervisor");

            }

            new SendEmail().Send(from,to,subject,body);

        }

        public IQueryable FindVouvherByClerk(string clerkID)
        {
            try
            {
                var vouchers = from voucher in context.AdjustmentVouchers
                               where voucher.ClerkID == clerkID
                               select new
                               {
                                   VoucherID=voucher.VoucherID,
                                   DateIssue=voucher.DateIssue,
                                   ApproveBy=voucher.ApproveBy,
                                   Status=voucher.Status,
                               };
                return vouchers;
            }
            catch(Exception ee)
            {
                return null;
            }
        }
    }
}
